package com.beyond.trace.controller.warn;

import com.beyond.trace.domain.pojo.warn.WarnRulePageQuery;
import com.beyond.trace.domain.warn.WarnRule;
import com.beyond.trace.service.warn.DeviceService;
import com.beyond.trace.service.warn.WarnRuleService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @ClassName: WarnRuleController
 * @author: z越
 * @date: 2023/2/24  15:49
 * @description 预警规则管理
 */
@RestController
@RequestMapping("/warn/rule")
@Api(tags = "预警规则管理")
public class WarnRuleController {
    private final WarnRuleService warnRuleService;
    private final DeviceService deviceService;

    @Autowired
    private RedisCache redisCache;

    public WarnRuleController(WarnRuleService warnRuleService,DeviceService deviceService) {
        this.warnRuleService = warnRuleService;
        this.deviceService = deviceService;
    }

    @PostMapping("/page")
    @ApiOperation("分页查询预警规则数据")
    public AjaxResult page(@io.swagger.v3.oas.annotations.parameters.RequestBody WarnRulePageQuery query){
        return AjaxResult.success(this.warnRuleService.findAllPages(query));
    }

    @GetMapping
    @ApiOperation("查询详细信息")
    public AjaxResult findOnly(Long id){
        return AjaxResult.success(this.warnRuleService.getById(id));
    }

    @PostMapping
    @ApiOperation("新增")
    public AjaxResult insert(@RequestBody WarnRule warnRule){
        warnRule.setCreateTime(new Date());
        warnRule.setDepartId(String.valueOf(SecurityUtils.getUserDepartId()));
        return AjaxResult.success(this.warnRuleService.save(warnRule));
    }

    @PutMapping
    @ApiOperation("修改")
    public AjaxResult update(@RequestBody WarnRule warnRule){
        if(warnRuleService.getById(warnRule.getId()) == null) {
            return AjaxResult.error("没有找到id为："+warnRule.getId()+" 的信息");
        }
        warnRule.setCreateTime(new Date());
        redisCache.deleteObject(warnRule.getDeviceNo()+":"+warnRule.getId());
        return AjaxResult.success(this.warnRuleService.updateById(warnRule));
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除数据（支持批量）")
    public AjaxResult delete( String ids){
        try {
            return AjaxResult.success(this.warnRuleService.batchDelete(ids));
        }catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @PutMapping("/open/{id}")
    @ApiOperation("开启规则")
    public AjaxResult openRule(@PathVariable Long id){
        WarnRule byId = warnRuleService.getById(id);
        if(byId == null) {
            return AjaxResult.error("没有找到规则信息");
        }
        redisCache.deleteObject(byId.getDeviceNo()+":"+byId.getId());
        return AjaxResult.success(this.warnRuleService.openRule(id));
    }

    @PutMapping("/off/{id}")
    @ApiOperation("关闭规则")
    public AjaxResult offRule(@PathVariable Long id){
        WarnRule byId = warnRuleService.getById(id);
        if(byId == null) {
            return AjaxResult.error("没有找到规则信息");
        }
        redisCache.deleteObject(byId.getDeviceNo()+":"+byId.getId());
        return AjaxResult.success(this.warnRuleService.offRule(id));
    }

    @GetMapping("/device")
    @ApiOperation("查询设备列表")
    public AjaxResult findAllByDeviceType(String deviceType){
        return  AjaxResult.success(this.deviceService.findAllByDeviceType(deviceType));
    }

}
